define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                 shop_id:0,
                 type:1,
                 id:0,
                 editData:{},
                 tags:[],
                 goods:[],
                 tag_name:''
                };
            },
            el: '#pageNewCate',
            created: function() {
                this.fetchData();
            },
              mounted: function() {
                    
                    if (this.mounted == 1) {
                        return;
                    }
                    
                 
                  
                this.mounted = 1;
            },



            methods: {
                fetchData: function() {
                    var _this = this;
                    this.shop_id = this.$route.params.shop_id;
                    
                    this.type = this.$route.params.type;
                    this.id = this.$route.params.id;
                    if(this.id>0){
                      this.initTag();
                       // this.loadGoods();
                    }else{
                      this.getTags();
                    }
                },
                select:function(){
                  var that = this;
                   var $eventSelect =$("#pageNewCate #tag_name");
                  $eventSelect.select2({
                      placeholder: '请输入分类名称',
                      allowClear: false,
                      tags: true,

                  });
                   $eventSelect.on("select2:select", function(e) {
                      var data = $(this).select2("data")[0];
                      if(Number(data.id)>0){
                        that.id = data.id;
                      }
                      that.saveTagName(data.text);
                   });
                },
                saveTagByInput:function(){
                  this.tag_name = $("#tag_name").val();
                  this.saveTagName(this.tag_name);
                },
                saveTagName:function(name){
                    var that = this;
                    app.post("mall/tag/add", {
                      name : name,
                      type:this.type,
                      id:this.id,
                      order_by:$("#order_by").val()?$("#order_by").val():999,
                      shop_id:this.shop_id
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      // if(that.id != res.data.id){
                      //   that.loadGoods();
                      //   that.id = res.data.id;
                      // }
                       that.loadGoods();
                      that.id = res.data.id;
                      that.editData = res.data;
                      app.setPageData("index",{reload:1});
                      $(".select2").remove();
                      // console.log( that.id);
                      
                      
                    });
                },
                getTags:function(){
                   var that = this ;
                    app.get("mall/tag/get", {
                      shop_id:this.shop_id
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.tags = res.data.list; 
                     Vue.nextTick(function(){
                        that.select();
                     });
                     
                    });
                },
                initTag:function(){
                    var that = this ;
                    app.get("mall/tag/find", {
                      id : this.id,
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.editData = res.data; 
                      // that.tag_name = res.data.name;
                    });
                },
                loadGoods:function(){
                    var that = this ;
                    that.goods=[];
                    app.get("mall/goods/get", {
                      attr_id : this.id,
                      shop_id:this.shop_id,
                      status:1,
                      limit:100
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.goods = res.data.list; 
                    });
                },
                showAct: function(goods_id,index){
                 
                  var that = this;
             
                  var params=[
                   
                    {
                      'text':'删除',
                      'methods':function(){
                          app.colseHandle();
                          that.delete(goods_id,index);
                          
                      }
                    }
                  ];
                  app.handle(params);

                     // _this.$router.push({ name: 'addMemberCard', params: { id: _this.cardInfo. } });
                     // { name: 'addMemberCard', params: { id: cardInfo.id ,appid:0 }}
                },
                delTag:function(){
                    var that = this ;
                    var goods = this.goods;
                    app.confirm({
                      text:'确认删除些分类吗？',
                      yes:function(){
                         app.get("mall/tag/del", {
                          id:that.id
                        }, function (res) {
                          if (res.code != 200) {
                             app.alert(res.msg);
                             return ;
                          }
                          app.setPageData("index",{reload:1});
                          that.$router.go(-1);
                        });
                      }
                    });
                   
                },
                delete:function(goods_id,index){
                   
                    var that = this ;
                    var goods = this.goods;

                    app.get("mall/goods/delTag", {
                      goods_id : goods_id,
                      shop_id:this.shop_id,
                      attr_id:this.id
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      app.setPageData("index",{reload:1});
                      goods.splice(index,1);
                      
                      
                    });
                },
                pageShow:function(){
                   // console.log('pageShow');
                   this.loadGoods();
                }
                


            }

        }).$mount('#app');
    };
    module.exports = controller;
});
